# Top level -*- makefile -*- fragment for libgccjit.so.
#   Copyright (C) 2013-2020 Free Software Foundation, Inc.

#This file is part of GCC.

#GCC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3, or (at your option)
#any later version.

#GCC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

# This file provides the language dependent support in the main Makefile.
# Each language makefile fragment must provide the following targets:
#
# foo.all.cross, foo.start.encap, foo.rest.encap,
# foo.install-common, foo.install-man, foo.install-info, foo.install-pdf,
# foo.install-html, foo.info, foo.dvi, foo.pdf, foo.html, foo.uninstall,
# foo.mostlyclean, foo.clean, foo.distclean,
# foo.maintainer-clean, foo.stage1, foo.stage2, foo.stage3, foo.stage4
#
# where `foo' is the name of the language.
#
# It should also provide rules for:
#
# - making any compiler driver (eg: g++)
# - the compiler proper (eg: cc1plus)
# - define the names for selecting the language in LANGUAGES.

#
# Define the names for selecting jit in LANGUAGES.
# Note that it would be nice to move the dependency on g++
# into the jit rule, but that needs a little bit of work
# to do the right thing within all.cross.

LIBGCCJIT_LINKER_NAME = libgccjit.so
LIBGCCJIT_VERSION_NUM = 0
LIBGCCJIT_MINOR_NUM = 0
LIBGCCJIT_RELEASE_NUM = 1
LIBGCCJIT_SONAME = $(LIBGCCJIT_LINKER_NAME).$(LIBGCCJIT_VERSION_NUM)
LIBGCCJIT_FILENAME = \
  $(LIBGCCJIT_SONAME).$(LIBGCCJIT_MINOR_NUM).$(LIBGCCJIT_RELEASE_NUM)

LIBGCCJIT_LINKER_NAME_SYMLINK = $(LIBGCCJIT_LINKER_NAME)
LIBGCCJIT_SONAME_SYMLINK = $(LIBGCCJIT_SONAME)

# Conditionalize the use of the LD_VERSION_SCRIPT_OPTION and
# LD_SONAME_OPTION depending if configure found them, using $(if)
# We have to define a COMMA here, otherwise the commas in the "true"
# result are treated as separators by the $(if).
COMMA := ,
LIBGCCJIT_VERSION_SCRIPT_OPTION = \
	$(if $(LD_VERSION_SCRIPT_OPTION),\
	  -Wl$(COMMA)$(LD_VERSION_SCRIPT_OPTION)$(COMMA)$(srcdir)/jit/libgccjit.map)

LIBGCCJIT_SONAME_OPTION = \
	$(if $(LD_SONAME_OPTION), \
	     -Wl$(COMMA)$(LD_SONAME_OPTION)$(COMMA)$(LIBGCCJIT_SONAME))

jit: $(LIBGCCJIT_FILENAME) \
	$(LIBGCCJIT_SYMLINK) \
	$(LIBGCCJIT_LINKER_NAME_SYMLINK) \
	$(FULL_DRIVER_NAME)

# Tell GNU make to ignore these if they exist.
.PHONY: jit

jit_OBJS = attribs.o \
	jit/dummy-frontend.o \
	jit/libgccjit.o \
	jit/jit-logging.o \
	jit/jit-recording.o \
	jit/jit-playback.o \
	jit/jit-result.o \
	jit/jit-tempdir.o \
	jit/jit-builtins.o \
	jit/jit-spec.o \
	gcc.o

# Use strict warnings for this front end.
jit-warn = $(STRICT_WARN)

# We avoid using $(BACKEND) from Makefile.in in order to avoid pulling
# in main.o
$(LIBGCCJIT_FILENAME): $(jit_OBJS) \
	libbackend.a libcommon-target.a libcommon.a \
	$(CPPLIB) $(LIBDECNUMBER) \
	$(LIBDEPS) $(srcdir)/jit/libgccjit.map \
	$(EXTRA_GCC_OBJS)
	+$(LLINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ -shared \
	     $(jit_OBJS) libbackend.a libcommon-target.a libcommon.a \
	     $(CPPLIB) $(LIBDECNUMBER) $(EXTRA_GCC_LIBS) $(LIBS) $(BACKENDLIBS) \
	     $(EXTRA_GCC_OBJS) \
	     $(LIBGCCJIT_VERSION_SCRIPT_OPTION) \
	     $(LIBGCCJIT_SONAME_OPTION)

$(LIBGCCJIT_SONAME_SYMLINK): $(LIBGCCJIT_FILENAME)
	ln -sf $(LIBGCCJIT_FILENAME) $(LIBGCCJIT_SONAME_SYMLINK)

$(LIBGCCJIT_LINKER_NAME_SYMLINK): $(LIBGCCJIT_SONAME_SYMLINK)
	ln -sf $(LIBGCCJIT_SONAME_SYMLINK) $(LIBGCCJIT_LINKER_NAME_SYMLINK)

#
# Build hooks:

jit.all.cross:
jit.start.encap:
jit.rest.encap:

# Documentation build hooks.
#
# The documentation can be built using the texinfo toolchain, or
# the sphinx toolchain
#
# The jit documentation is authored using Sphinx, which has numerous
# advantages over Texinfo, including:
#
#   * much faster
#
#   * use of CSS and JS to provide less of a 1990s feel in the generated
#     HTML.
#
#   * sane, stable HTML page and anchor names
#
#   * sane HTML navigation: ability to move forward and back in the HTML
#     at every node to read the HTML like a book
#
#   * syntax-coloring of examples
#
#   * the ability to "include" fragments of code inline.  This is used
#     heavily by the jit docs, so that the example code is shared by both
#     the test suite and the documentation to ensure that the examples
#     appearing in the docs actually compile and work
#
# Sphinx is not a "blessed" dependency, and so a prebuilt libgccjit.texinfo
# file built by Sphinx is checked into the source tree to avoid requiring
# everyone to have Sphinx installed.
#
# This prebuilt libgccjit.texinfo has the "include" fragments "baked in",
# and so contains the content from the sphinx toolchain, but lacks the
# syntax-coloring, and the generated HTML is (IMHO) greatly inferior to
# that generated by Sphinx.

# These targets redirect HTML creation and installation to either
# jit.sphinx.(install-)html or jit.texinfo.(install-)html.
jit.html: jit.$(doc_build_sys).html
jit.install-html: jit.$(doc_build_sys).install-html

# For now, use texinfo for pdf, since the sphinx latex toolchain currently
# fails for me deep inside pdflatex (see notes below)
jit.pdf: jit.texinfo.pdf
jit.install-pdf: jit.texinfo.install-pdf

# Hooks for building docs using texinfo
JIT_TEXI_FILES = $(srcdir)/jit/docs/_build/texinfo/libgccjit.texi

jit.info: doc/libgccjit.info
doc/libgccjit.info: $(JIT_TEXI_FILES)
	if test "x$(BUILD_INFO)" = xinfo; then \
	  rm -f doc/libgccjit.info*; \
	  $(MAKEINFO) $(MAKEINFOFLAGS) -I $(gcc_docdir) \
		-I $(gcc_docdir)/include -o $@ $<; \
	else true; fi

jit.install-info: $(DESTDIR)$(infodir)/libgccjit.info

jit.dvi: doc/libgccjit.dvi
doc/libgccjit.dvi: $(JIT_TEXI_FILES)
	$(TEXI2DVI) -I $(abs_docdir) -I $(abs_docdir)/include -o $@ $<

jit.texinfo.html: $(build_htmldir)/jit/index.html

$(build_htmldir)/jit/index.html: $(srcdir)/jit/docs/_build/texinfo/libgccjit.texi
	$(mkinstalldirs) $(@D)
	rm -f $(@D)/*
	$(TEXI2HTML) -I $(gcc_docdir)/include -I $(srcdir)/jit -o $(@D) $<

jit.texinfo.install-html: jit.texinfo.html
	@$(NORMAL_INSTALL)
	test -z "$(htmldir)" || $(mkinstalldirs) "$(DESTDIR)$(htmldir)"
	@for p in $(build_htmldir)/jit; do \
	  if test -f "$$p" || test -d "$$p"; then d=""; else d="$(srcdir)/"; fi; \
	  f=$(html__strip_dir) \
	  if test -d "$$d$$p"; then \
	    echo " $(mkinstalldirs) '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(mkinstalldirs) "$(DESTDIR)$(htmldir)/$$f" || exit 1; \
	    echo " $(INSTALL_DATA) '$$d$$p'/* '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p"/* "$(DESTDIR)$(htmldir)/$$f"; \
	  else \
	    echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(htmldir)/$$f"; \
	  fi; \
	done

jit.texinfo.pdf: doc/libgccjit.pdf

doc/libgccjit.pdf: $(JIT_TEXI_FILES)
	$(TEXI2PDF) -I $(abs_docdir) -I $(abs_docdir)/include -o $@ $<

jit.texinfo.install-pdf: doc/libgccjit.pdf
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@for p in doc/libgccjit.pdf; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

# Hooks for building docs using the Sphinx toolchain:

SPHINX_BUILD_DIR=jit/sphinx-build

jit.sphinx.html:
	mkdir -p $(SPHINX_BUILD_DIR)
	(cd $(srcdir)/jit/docs && \
	  make html BUILDDIR=$(PWD)/$(SPHINX_BUILD_DIR) )

jit_htmldir=$(htmldir)/jit

jit.sphinx.install-html: jit.sphinx.html
	@$(NORMAL_INSTALL)
	test -z "$(jit_htmldir)" || $(mkinstalldirs) "$(DESTDIR)$(jit_htmldir)"
	@for f in $(shell cd $(SPHINX_BUILD_DIR)/html && find) ; do \
	  if test -f $(SPHINX_BUILD_DIR)/html/"$$f"; then \
	     $(INSTALL_DATA) $(SPHINX_BUILD_DIR)/html/"$$f" $(DESTDIR)$(jit_htmldir)/"$$f"; \
	  else \
	     mkdir $(DESTDIR)$(jit_htmldir)/"$$f"; \
	  fi; \
	done

# (This one is currently failing deep inside pdflatex for me;
# see https://bugzilla.redhat.com/show_bug.cgi?id=1148845 )
jit.sphinx.pdf: $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf
$(SPHINX_BUILD_DIR)/latex/libgccjit.pdf:
	mkdir -p $(SPHINX_BUILD_DIR)
	(cd $(srcdir)/jit/docs && \
	  make latexpdf BUILDDIR=$(PWD)/$(SPHINX_BUILD_DIR) )

jit.sphinx.install-pdf: $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@for p in $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

jit.srcinfo:
jit.srcextra:

jit.tags:

jit.man:

jit.srcman:

lang_checks += check-jit
lang_checks_parallelized += check-jit
# This number is somewhat arbitrary.  Two tests are much slower
# than all the others (test-combination.c and test-threads.c) so
# we want them to be placed in different "buckets".
check_jit_parallelize = 10

# No jit-specific selftests
selftest-jit:

#
# Install hooks:
jit.install-common: installdirs
	$(INSTALL_PROGRAM) $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)/$(libdir)/$(LIBGCCJIT_FILENAME)
	ln -sf \
	  $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)/$(libdir)/$(LIBGCCJIT_SONAME_SYMLINK)
	ln -sf \
	  $(LIBGCCJIT_SONAME_SYMLINK)\
	  $(DESTDIR)/$(libdir)/$(LIBGCCJIT_LINKER_NAME_SYMLINK)
	$(INSTALL_DATA) $(srcdir)/jit/libgccjit.h \
	  $(DESTDIR)/$(includedir)/libgccjit.h
	$(INSTALL_DATA) $(srcdir)/jit/libgccjit++.h \
	  $(DESTDIR)/$(includedir)/libgccjit++.h

jit.install-man:

jit.install-plugin:

jit.uninstall:

#
# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

jit.mostlyclean:
	-rm -f $(LIBGCCJIT_FILENAME) $(LIBGCCJIT_SYMLINK)
	-rm -f $(LIBGCCJIT_LINKER_NAME_SYMLINK) $(FULL_DRIVER_NAME)
	-rm -f $(LIBGCCJIT_SONAME)
	-rm -f $(jit_OBJS)

jit.clean:

jit.distclean:

jit.maintainer-clean:

#
# Stage hooks:
# The main makefile has already created stage?/jit.

jit.stage1: stage1-start
	-mv jit/*$(objext) stage1/jit
jit.stage2: stage2-start
	-mv jit/*$(objext) stage2/jit
jit.stage3: stage3-start
	-mv jit/*$(objext) stage3/jit
jit.stage4: stage4-start
	-mv jit/*$(objext) stage4/jit
jit.stageprofile: stageprofile-start
	-mv jit/*$(objext) stageprofile/jit
jit.stagefeedback: stagefeedback-start
	-mv jit/*$(objext) stagefeedback/jit
